﻿using System;
using System.Runtime.Serialization;
using GalaSoft.MvvmLight.Command;

using XamlingCore.Infrastructure.View.Implementation;
using XamlingCore.Infrastructure.View.Interface;
using XamlingCore.WP.Infrastructure.View.Interface;

namespace XamlingCore.Infrastructure.View
{
    [DataContract]
    public abstract class XContentViewModelBase : ViewModelBase, IActiveAware, IReactivate
    {
        private XFrameViewModelBase parentModel;

        public RelayCommand NavigateBackCommand { get; set; }

        private string title;
        private string description;

        protected internal bool IsReady = false;

        protected internal object NavigationParameter { get; set; }

        //[DataMember]
        //public object Tag { get; set; }

        [DataMember]
        public Guid ParentModelGuid { get; set; }

        protected XContentViewModelBase()
        {
            Guid = Guid.NewGuid();
            NavigateBackCommand = new RelayCommand(NavigateBack);
        }

        public T CreateChildFrame<T>(Action<T> initialisedCallback)
            where T : XFrameViewModelBase, new()
        {
            return ParentModel.CreateChildFrame<T>(initialisedCallback);
        }

        public T CreateChildFrame<T>()
            where T : XFrameViewModelBase, new()
        {
            return ParentModel.CreateChildFrame<T>(null);
        }

        public T CreateContentModel<T>(Action<T> initialisedCallback)
             where T : XContentViewModelBase, new()
        {
            return ParentModel.CreateContentModel<T>(initialisedCallback);
        }

        public T CreateContentModel<T>()
             where T : XContentViewModelBase, new()
        {
            return ParentModel.CreateContentModel<T>(null);
        }

        public XContentViewModelBase CreateAndNavigate(string typeName, object navParameter)
        {

            var model = ParentModel.CreateContentModel(typeName, p =>
                                                  {
                                                      NavigationParameter = navParameter;
                                                  });
            return model;
        }

        public virtual void OnInitialise() { }
        public virtual void OnReactivate() { }
        public virtual void OnViewDeactivated() { }
        //internal protected virtual void OnDeactivate() { }
        //internal protected virtual void OnBeforeActivate() { }

        [DataMember]
        public string Title
        {
            get { return title; }
            set
            {
                title = value;
                OnPropertyChanged("Title");
            }
        }

        [DataMember]
        public string Description
        {
            get { return description; }
            set
            {
                description = value;
                OnPropertyChanged("Description");
            }
        }

        [DataMember]
        public Guid Guid { get; set; }

        public bool IsLoading
        {
            get
            {
                return ParentModel.IsLoading;
            }
            set
            {
                ParentModel.IsLoading = value;
            }
        }

        public XFrameViewModelBase ParentModel
        {
            get
            {
                if (parentModel != null)
                {
                    return parentModel;
                }
                else
                {
                    var model = XFrameViewModelBase.RootFrame.FindModel(ParentModelGuid);
                    parentModel = model;
                    return model;
                }

            }
            set
            {
                parentModel = value;
                ParentModelGuid = ParentModel.Guid;
            }
        }

        #region NavigationShortcuts
        public void NavigateTo(object content)
        {
            ParentModel.NavigateTo(content);
        }

        protected void NavigateTo(object content, bool noHistory, bool forceBack)
        {
            ParentModel.NavigateTo(content, noHistory, forceBack);
        }

        protected void NavigateTo(object content, bool noHistory)
        {
            ParentModel.NavigateTo(content, noHistory);
        }

        protected void NavigateBack()
        {
            ParentModel.NavigateBack();
        }

        protected void NavigateBack(bool allowNullNavigation)
        {
            ParentModel.NavigateBack(allowNullNavigation);
        }
        #endregion

        #region ShowDialogShortcuts


        public override void ShowDialog(ShowDialogViewModelBase dialogViewModel, System.TimeSpan timeOut, System.Action<Dialog.DialogResult> dialogResult)
        {
            ParentModel.ShowDialog(dialogViewModel, timeOut, dialogResult);
        }

        public override void HideDialog()
        {
            ParentModel.HideDialog();
        }

        #endregion

        #region IActiveAware Members

        public virtual void IsActive(bool activated)
        {
        }

        #endregion
    }
}
